package org.lansir.af.dataview;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import org.lansir.af.database.DataRowActivity;
import org.lansir.af.database.Database;
import org.lansir.af.ui.CommonFragment;
import org.lansir.af.util.RepeatData;
import org.lansir.af.util.RepeatDataValue;

import android.database.Cursor;
import android.util.Log;

/**
 * 
 * @author 蓝智宏
 *
 */
public class DataViewActivity extends DataView {

	private Calendar calUtilDate = Calendar.getInstance();
	// fields
	

	// fields
	private RepeatData repeat = new RepeatData();

	// fields
	protected Calendar calStartDateForCache = Calendar.getInstance();
	protected RepeatDataValue[] vecDateRangeCache = new RepeatDataValue[42];

	// fields
	private Calendar calItemStartDate = Calendar.getInstance();

	// fields
	private DataViewItemActivityComparator fnCmp = null;

	

	public DataViewActivity(Database db) {
		super(db);
		sTableName = Database.sTableNameActivities;
		fnCmp = new DataViewItemActivityComparator();
		// initialize date value cache
		for (int iDay = 0; iDay < 42; iDay++) {
			vecDateRangeCache[iDay] = new RepeatDataValue();
		}
	}

	public int getDaysRangeForView(final int dayType) {
		if (dayType == CommonFragment.ViewType.NONE)
			return 0;
		if (dayType == CommonFragment.ViewType.TODAY_ALARM)
			return 1;

		if (dayType == CommonFragment.ViewType.DAY)
			return 0;

		if (dayType == CommonFragment.ViewType.WEEK)
			return 7;

		if (dayType == CommonFragment.ViewType.MONTH)
			return 42;

		return 0;
	}

	@Override
	protected void addItem(Cursor cr) {
		DataViewItemActivity item = new DataViewItemActivity();
		item.setID(cr.getLong(DataRowActivity.fid.ID));
		item.setSubject(cr.getString(DataRowActivity.fid.Subject));
		item.setLocation(cr.getString(DataRowActivity.fid.Location));
		item.setOrganizer(cr.getString(DataRowActivity.fid.Organizer));
		item.setStartDate(calUtilDate, cr
				.getLong(DataRowActivity.fid.StartDate));
		item.setAlarm(cr.getLong(DataRowActivity.fid.Alarm) == 1);
		item.setSnooze(cr.getInt(DataRowActivity.fid.Snooze));
		item.setSnoozeCount(cr.getInt(DataRowActivity.fid.SnoozeCount));
		item.setActionClear(cr.getInt(DataRowActivity.fid.ActionClear));

		item.setRepeatType(cr.getInt(DataRowActivity.fid.RepeatType));
		item.setRingerMode(cr.getInt(DataRowActivity.fid.RingerMode));

		//Log.v("ActivityFairy", "Hour:" + calUtilDate.get(Calendar.HOUR_OF_DAY) + ",Minute:" + calUtilDate.get(Calendar.MINUTE));
		if (!cr.isNull(DataRowActivity.fid.RepeatEvery))
			item.setRepeatEvery(cr.getInt(DataRowActivity.fid.RepeatEvery));

		if (!cr.isNull(DataRowActivity.fid.RepeatEndOnDate))
			item.setRepeatEndOnDate(cr
					.getLong(DataRowActivity.fid.RepeatEndOnDate));
		
		rows.add(item);
	}

	@Override
	protected void filterDataForView(DataViewItem item, Calendar calStartDate,
			final int iDayType) {
		// set repeat comparer data
		DataViewItemActivity itemActivity = (DataViewItemActivity) item;
		repeat.setRepeatTypeAsInt(itemActivity.getRepeatType());
		repeat.setEndOnDate(null);
		repeat.setEvery(1);

		// if repeat set
		if (itemActivity.isRepeat()) {
			repeat.setEvery(itemActivity.getRepeatEvery());
			if (itemActivity.useRepeatEndOnDate())
				repeat.setEndOnDate(itemActivity.getRepeatEndOnDate());
		}

		// get appt item start date
		calItemStartDate.setTimeInMillis(itemActivity.getStartDateAsLong());
		// calItemStartDate.setFirstDayOfWeek(prefs.iFirstDayOfWeek);
		repeat.setStartDate(calItemStartDate);

		// filter item for date range
		if (iDayType != CommonFragment.ViewType.NONE) {
			itemActivity.clear();
			//Log.v("ActivityFairy", "iDayType:" + iDayType);
			final int iDaysCount = getDaysRangeForView(iDayType);
			//NONE
			if (iDaysCount == 0) {
				if (repeat.isDateEqual(calStartDate)) {
					itemActivity.setVisibleDay(0);
					itemActivity.setDayType(iDayType);
					filteredRows.add(itemActivity);
				}
			//TODAY_ALARM
			} else if (iDaysCount == 1) {
				if (repeat.isDateEqual(calStartDate) && itemActivity.isAlarm()) {
					itemActivity.setVisibleDay(0);
					itemActivity.setDayType(iDayType);
					filteredRows.add(itemActivity);
					//Log.v("ActivityFairy", "FilterRowsAdd:TodayAlarms:" + filteredRows.size());
				}	
			} else {
				//Log.v("ActivityFairy", "FilterRowsAdd:Activity:" + itemActivity.getSubject() + "," + itemActivity.getStartDate().get(Calendar.MONTH));
				RepeatDataValue dvDate = null;
				boolean needAddFlag = false;
				for (int iDay = 0; iDay < iDaysCount; iDay++) {
					dvDate = vecDateRangeCache[iDay];

					if (repeat.IsDateEqual(dvDate)) {
						itemActivity.setVisibleDay(iDay);
						itemActivity.setDayType(iDayType);
						needAddFlag = true;
					}

				}
				if(needAddFlag == true)
					filteredRows.add(itemActivity);
				Log.v(DataView.TAG, "FilterRowsAdd:Week:" + filteredRows.size());
			}
			
		}
	}

	@Override
	protected void filterDataPrepare(Calendar calStartDate, final int iDayType) {
		final int iDaysCount = getDaysRangeForView(iDayType);

		int iDayValue = 1;
		// cache date values backward for alarm service

		calStartDateForCache.setTimeInMillis(calStartDate.getTimeInMillis());
		for (int iDay = 0; iDay < (iDaysCount); iDay++) {
			vecDateRangeCache[iDay].getFromCalendar(calStartDateForCache);
			calStartDateForCache.add(Calendar.DAY_OF_YEAR, iDayValue);
		}
	}

	@Override
	protected void sortView() {
		Collections.sort(rows, fnCmp);
		Collections.sort(filteredRows, fnCmp);
		//TODO
//		for (Map.Entry<CalendarKey, List<DataViewItem>> entry : mapDayRowsForView
//				.entrySet()) {
//			List<DataViewItem> l = entry.getValue();
//			Collections.sort(l, fnCmp);
//			entry.setValue(l);
//		}
	}
	
	public List<DataViewItemActivity> getAppointlistForAlarm(Calendar calStartDate) {
		this.filterData(calStartDate, CommonFragment.ViewType.TODAY_ALARM);
		List<DataViewItemActivity> items = new ArrayList<DataViewItemActivity>();
		for (int i = 0; i < rows.size(); i++) {
			DataViewItemActivity activityRow = (DataViewItemActivity) rows
					.get(i);
			if (activityRow.getDayType() == CommonFragment.ViewType.TODAY_ALARM) {
				items.add(activityRow);
			}
		}
		// DataViewItem dw = this.GetRow(0, viewMode.DAY);
		// Log.v("LAN_DEBUG", "start_date:" + dw.GetStartDateAsLong());

		return items;

	}
}
